import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * Event - 事件服务
 * @date: 2018-6-20

 */
import request from 'utils/request';
import { CPAAS_PLATFORM, CPAAS_MSG } from 'utils/config';
import { parseParameters, isTenantRoleLevel, getCurrentOrganizationId } from 'utils/utils';
var isSiteFlag = !isTenantRoleLevel();
var tenantId = getCurrentOrganizationId();

function eventApi() {
  return !isSiteFlag ? "".concat(CPAAS_PLATFORM, "/v1/").concat(tenantId, "/events") : "".concat(CPAAS_PLATFORM, "/v1/events");
}

function messageApi() {
  return !isSiteFlag ? "".concat(tenantId, "/message-events") : "message-events";
}
/**
 * 查询事件列表.
 * @param {Object} params 查询条件
 * @returns {Object} 返回结果
 */


function queryEvents() {
  return _queryEvents.apply(this, arguments);
}
/**
 * 根据ID查询具体事件数据.
 * @param {number} id 事件 ID
 * @returns {Object} 返回结果
 */


function _queryEvents() {
  _queryEvents = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var params,
        query,
        _args = arguments;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            params = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
            query = parseParameters(params, {
              field: 'eventCode',
              order: 'asc'
            });
            return _context.abrupt("return", request(eventApi(), {
              method: 'GET',
              query: query
            }));

          case 3:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _queryEvents.apply(this, arguments);
}

function getEvent(_x) {
  return _getEvent.apply(this, arguments);
}
/**
 * 批量删除事件.
 * @param {Object} params 待删除数据
 */


function _getEvent() {
  _getEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(eventApi(), "/").concat(params.id)));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _getEvent.apply(this, arguments);
}

function remove(_x2) {
  return _remove.apply(this, arguments);
}
/**
 * 创建事件.
 * @param {Object} params 事件
 */


function _remove() {
  _remove = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var selectedRows;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            selectedRows = params.selectedRows;
            return _context3.abrupt("return", request(eventApi(), {
              method: 'DELETE',
              body: selectedRows
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _remove.apply(this, arguments);
}

function addEvent(_x3) {
  return _addEvent.apply(this, arguments);
}
/**
 * 更新事件.
 * @param {Object} params 事件
 */


function _addEvent() {
  _addEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request(eventApi(), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _addEvent.apply(this, arguments);
}

function updateEvent(_x4) {
  return _updateEvent.apply(this, arguments);
}
/**
 * 更新事件规则.
 * @param {Object} params 事件规则
 */


function _updateEvent() {
  _updateEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request(eventApi(), {
              method: 'PUT',
              body: params
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _updateEvent.apply(this, arguments);
}

function updateRule(_x5) {
  return _updateRule.apply(this, arguments);
}
/**
 * 批量删除事件规则.
 * @param {Object} params 事件规则
 */


function _updateRule() {
  _updateRule = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request("".concat(eventApi(), "/").concat(params.eventId, "/rules"), {
              method: params.eventRuleId ? 'PUT' : 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _updateRule.apply(this, arguments);
}

function removeRule(_x6) {
  return _removeRule.apply(this, arguments);
}
/**
 * 查询消息列表.
 * @param {Object} params 查询条件
 * @returns {Object} 返回结果
 */


function _removeRule() {
  _removeRule = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(eventApi(), "/").concat(params.eventId, "/rules"), {
              method: 'DELETE',
              body: params.selectedRows
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _removeRule.apply(this, arguments);
}

function queryMessages(_x7) {
  return _queryMessages.apply(this, arguments);
}
/**
 * 新增消息.
 * @param {Object} params 事件消息参数
 * @returns {Object} 返回结果
 */


function _queryMessages() {
  _queryMessages = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(params) {
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            return _context8.abrupt("return", request("".concat(CPAAS_MSG, "/v1/").concat(messageApi()), {
              method: 'GET',
              query: params
            }));

          case 1:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _queryMessages.apply(this, arguments);
}

function createMessage(_x8) {
  return _createMessage.apply(this, arguments);
}
/**
 * 更新消息
 * @param {Object} params 事件消息参数
 * @returns {Object} 返回结果
 */


function _createMessage() {
  _createMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(params) {
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            return _context9.abrupt("return", request("".concat(CPAAS_MSG, "/v1/").concat(messageApi()), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _createMessage.apply(this, arguments);
}

function updateMessage(_x9) {
  return _updateMessage.apply(this, arguments);
}
/**
 * 批量删除事件消息.
 * @param {Object} params 事件消息参数
 */


function _updateMessage() {
  _updateMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(params) {
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            return _context10.abrupt("return", request("".concat(CPAAS_MSG, "/v1/").concat(messageApi()), {
              method: 'PUT',
              body: params
            }));

          case 1:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _updateMessage.apply(this, arguments);
}

function deleteMessages(_x10) {
  return _deleteMessages.apply(this, arguments);
}

function _deleteMessages() {
  _deleteMessages = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(params) {
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            return _context11.abrupt("return", request("".concat(CPAAS_MSG, "/v1/").concat(messageApi(), "/batch-remove"), {
              method: 'DELETE',
              body: params.messageEvents
            }));

          case 1:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _deleteMessages.apply(this, arguments);
}

export default {
  queryEvents: queryEvents,
  getEvent: getEvent,
  remove: remove,
  addEvent: addEvent,
  updateEvent: updateEvent,
  updateRule: updateRule,
  removeRule: removeRule,
  queryMessages: queryMessages,
  createMessage: createMessage,
  updateMessage: updateMessage,
  deleteMessages: deleteMessages
};